#-*- coding:utf8 -*-
from makingrec import sim_pearson
from makingrec import sim_distance
critics = {'Lisa Rose':{'Lady in the Water':2.5,'Snakes on a Plane':3.5,'Just My Luck':3.0,'Superman Return':3.5,
'You,Me and Dupree':2.5,'The Night Listener':3.0},
'Gene Seymour':{'Lady in the Water':3.0,'Snakes on a Plane':3.5,'Just My Luck':1.5,'Superman Return':5.0,
'You,Me and Dupree':3.5,'The Night Listener':3.0},
'Michael Phillips':{'Lady in the Water':2.5,'Snakes on a Plane':3.0,'Superman Return':3.5,'The Night Listener':4.0},
'Claudia Puig':{'Snakes on a Plane':3.5,'Just My Luck':3.0,'Superman Return':4.0,
'You,Me and Dupree':2.5,'The Night Listener':4.5},
'Mick LaSalle':{'Lady in the Water':3.0,'Snakes on a Plane':4.0,'Just My Luck':2.0,'Superman Return':3.0,
'You,Me and Dupree':2.0,'The Night Listener':3.0},
'Jack Matthews':{'Lady in the Water':3.0,'Snakes on a Plane':4.0,'Superman Return':5.0,
'You,Me and Dupree':3.5,'The Night Listener':3.0},
'Toby':{'Snakes on a Plane':4.5,'Superman Return':4.0,'You,Me and Dupree':1.0},
}
# 找到与person相似度对高的n个人
def topMatches(prefs,person,n=5,similarity=sim_pearson):
    scores = [(similarity(prefs,person,other),other) for other in prefs if other!=person]
    scores.sort()
    scores.reverse()
    return scores[0:n]

# 猜你喜欢(基于用户)
def getRecommendation(prefs,person,similarity=sim_pearson):
    # 相似度加权评分（评分乘以该用户的相似度）的总和
    total={}
    # 所有拥有该项目的相似度的总和
    simSums={}
    for other in prefs:
        if other == person : continue
        # 计算相似度
        sim = similarity(prefs,person,other)
        if sim <=0 : continue
        # 找出该用户有而本人没有的项目
        for item in prefs[other]:
            if item in prefs[person]: continue
            total.setdefault(item,0)
            total[item]+=prefs[other][item]*sim
            simSums.setdefault(item,0)
            simSums[item]+=sim
    rankings = [(t/simSums[item],item) for item,t in total.items()]
    rankings.sort()
    rankings.reverse()
    return rankings

# print getRecommendation(critics,'Toby')
# 匹配商品(假设获取的数据是物品，查询物品的彼此相近)
# 键值对调
def transformPerfs(prefs):
    result={}
    for person in prefs:
        for item in prefs[person]:
            result.setdefault(item,{})
            result[item][person]=prefs[person][item]

    return result

# print getRecommendation(transformPerfs(critics),'Just My Luck')

# 基于物品的协作型过滤，构造相近物品的完整数据集
def calculateSimilarItems(prefs,n=10):
    result={}
    itemPrefs=transformPerfs(prefs)
    for item in itemPrefs:
        score=topMatches(itemPrefs,item,n,sim_distance)
        result[item]=score
    return result

# print calculateSimilarItems(critics)

# 猜你喜欢（基于商品）
def getRecommendedItems(prefs,itemMatch,user):
    userRatings = prefs[user]
    scores = {}
    totalSim = {}
    for item,rating in userRatings.items():
        for similarity,item2 in itemMatch[item]:
            if item2 in userRatings : continue
            scores.setdefault(item2,0)
            scores[item2]+=similarity*rating

            totalSim.setdefault(item2,0)
            totalSim[item2]+=similarity

    rankings = [(score/totalSim[item],item) for item,score in scores.items()]

    rankings.sort()
    rankings.reverse()
    return rankings

print getRecommendedItems(critics,calculateSimilarItems(critics),'Toby')